package main

import (
	"fmt"
	"log"
	"net"
	"os"
//	"sync"
)

//var wg sync.WaitGroup
var serverIP = "zichuandr.xyz"
var serverPort = "80"

func main() {
	for {
		conn, err := net.Dial("tcp", serverIP + ":" + serverPort)
		myerr(err)
//		wg.Add(1)
		go writeToServer(conn)
//		wg.Wait()
	}
}

//从stdin读取数据，写入到server
func writeToServer(c net.Conn) {
	fmt.Println("Dbug-> writeToServer--begin")
	b := make([]byte, 200)
	_, err := os.Stdin.Read(b)
	myerr(err)
	_, err = c.Write(b)
	myerr(err)
	fmt.Println("Dbug-> WriteToServer--over")
	showResponse(c)
}

//把从server接收到的数据打印到stdout
func showResponse(c net.Conn) {
	fmt.Println("Dbug-> showResponse--begin")
	b := make([]byte, 200)
	_, err := c.Read(b)
	myerr(err)
	//	fmt.Println("Dbug->b:", string(b))
	_, err = os.Stdout.Write(b)
	//	_, err = io.Copy(os.Stdout, c)
	myerr(err)
	fmt.Println("Dbug-> showResponse--over")
//	wg.Done()
}

//错误处理
func myerr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
